package com.networklib.task.impl;

import android.content.Context;
import android.os.Environment;

import com.networklib.TaskResult;
import com.networklib.http.exception.NetException;
import com.networklib.http.other.HttpMethod;

import java.io.File;
import java.io.IOException;
import java.util.TimeZone;

import hirondelle.date4j.DateTime;
import okhttp3.Response;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;

/**
 * Created by ASUS on 2017/6/14.
 */

public class DownloadHttpTask extends SimpleHttpTask {
    private String saveFilePath;
    private String saveFileName;
    private boolean downloadFlag = false;
    private File saveFile;

    public DownloadHttpTask(Context context,String url, HttpMethod method, String saveFilePath, String saveFileName) throws NetException {
        super(context,url, method, null);
        this.saveFilePath = saveFilePath;
        this.saveFileName = saveFileName;
    }

    public boolean before(TaskResult taskResult) {
        if(super.before(taskResult)) {
            String state = Environment.getExternalStorageState();
            if ("mounted".equals(state)) {
                File storageDirectory = Environment.getExternalStorageDirectory();
                long space = storageDirectory.getUsableSpace();
                if (space > 104857600L) {
                    File saveDirectory = new File(storageDirectory.getAbsolutePath() + this.saveFilePath);

                    try {
                        this.downloadFlag = saveDirectory.exists() || saveDirectory.mkdirs();
                    } catch (Exception var10) {
                        this.setResultFailure(taskResult, var10);
                    }

                    if (this.downloadFlag) {
                        this.downloadFlag = false;
                        this.saveFile = new File(saveDirectory.getAbsolutePath() + "/" + this.saveFileName);
                        if (this.saveFile.exists()) {
                            long e = DateTime.now(TimeZone.getDefault()).getMilliseconds(TimeZone.getDefault());
                            this.saveFile = new File(saveDirectory.getAbsolutePath() + "/" + e + this.saveFileName);
                        }

                        try {
                            this.downloadFlag = this.saveFile.createNewFile();
                        } catch (IOException var9) {
                            this.removeDownloadFile();
                            this.setResultFailure(taskResult, var9);
                        }
                    }
                } else {
                    this.setResultFailure(taskResult, "存储空间不足");
                }
            } else {
                this.setResultFailure(taskResult, "SD卡未插入");
            }
            return this.downloadFlag;
        }
        return false;
    }

    public void after(TaskResult taskResult) {
        try {
            Response e = (Response) taskResult.getResult();
            BufferedSink bufferedSink = Okio.buffer(Okio.sink(this.saveFile));
            BufferedSource bufferedSource = e.body().source();
            bufferedSink.writeAll(bufferedSource);
            bufferedSink.flush();
            bufferedSink.close();
            bufferedSource.close();
            this.setSuccessResult(taskResult, this.saveFile);
        } catch (Exception var5) {
            this.setResultFailure(taskResult, var5);
        }

    }

    public boolean removeDownloadFile() {
        return this.saveFile != null && this.saveFile.exists() ? this.saveFile.delete() : true;
    }
}
